/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ufcg.dsc.lsi.vademecum;

import br.edu.ufcg.dsc.lsi.vademecum.context.provider.RawDataProviderIF;
import br.edu.ufcg.dsc.lsi.vademecum.services.PresentationService;
import javax.microedition.location.LocationException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.LocationProvider;
import javax.microedition.pim.PIM;
import org.netbeans.microedition.lcdui.pda.PIMBrowser;

/**
 * The main VadeMecum Client class
 * @author Hugo Feitosa de Figueirêdo
 */
public class VadeMecumClientMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command okCommand;
    private Command exitCommand1;
    private Command mapScreenCommand;
    private Form loginForm;
    private TextField password;
    private TextField login;
    private Alert alert;
    private Form welcomeForm;
    private ImageItem imageItem;
    private PIMBrowser pimBrowser;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public VadeMecumClientMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        int index = 0;
        String appProperty = getAppProperty("Sensor-" + index++);
        while (appProperty != null) {
            try {
                Class provider = Class.forName(
                        "br.edu.ufcg.dsc.lsi.vademecum.context.provider."
                        + appProperty
                        + "RawDataProvider");
                try {
                    RawDataProviderIF providerInstance = (RawDataProviderIF) provider.newInstance();
                    providerInstance.run();
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                }
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            appProperty = getAppProperty("sensor-" + index++);
        }
//GEN-LINE:|0-initialize|1|0-postInitialize
    // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getLoginForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
    // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
    // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == loginForm) {//GEN-BEGIN:|7-commandAction|1|19-preAction
            if (command == exitCommand) {//GEN-END:|7-commandAction|1|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|2|19-postAction
            // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|3|22-preAction
                // write pre-action user code here
                loginValidation();//GEN-LINE:|7-commandAction|4|22-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|41-preAction
        } else if (displayable == pimBrowser) {
            if (command == PIMBrowser.SELECT_PIM_ITEM) {//GEN-END:|7-commandAction|5|41-preAction
                // write pre-action user code here
                switchDisplayable(null, getWelcomeForm());//GEN-LINE:|7-commandAction|6|41-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|37-preAction
        } else if (displayable == welcomeForm) {
            if (command == exitCommand1) {//GEN-END:|7-commandAction|7|37-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|8|37-postAction
            // write post-action user code here
            } else if (command == mapScreenCommand) {//GEN-LINE:|7-commandAction|9|50-preAction


                Thread mapLoad = new Thread() {

                    public void run() {
                        try {
                            VadeMecumMIDPCanvas canvas = VadeMecumMIDPCanvas.getInstance();
                            int width = canvas.getWidth();
                            int height = canvas.getHeight();
                            PresentationService gMap = PresentationService.getInstance();
                            gMap.setWidth(width);
                            gMap.setHeight(height);
                            Image map = gMap.retrieveStaticImage(-7, -35);
                            showMap(map);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                };
                mapLoad.start();


//GEN-LINE:|7-commandAction|10|50-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|7-postCommandAction
        }//GEN-END:|7-commandAction|11|7-postCommandAction
    // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|12|
    //</editor-fold>//GEN-END:|7-commandAction|12|

    public void showMap(Image map) {
        VadeMecumMIDPCanvas canvas = VadeMecumMIDPCanvas.getInstance();
        canvas.setMap(map);
        Display.getDisplay(this).setCurrent(canvas);
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
        // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loginForm ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of loginForm component.
     * @return the initialized component instance
     */
    public Form getLoginForm() {
        if (loginForm == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            loginForm = new Form("Welcome", new Item[] { getLogin(), getPassword() });//GEN-BEGIN:|14-getter|1|14-postInit
            loginForm.addCommand(getExitCommand());
            loginForm.addCommand(getOkCommand());
            loginForm.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
        // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return loginForm;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|21-getter|1|21-postInit
        // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|21-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: login ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of login component.
     * @return the initialized component instance
     */
    public TextField getLogin() {
        if (login == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            login = new TextField("login", "Hugo", 32, TextField.ANY);//GEN-BEGIN:|24-getter|1|24-postInit
            login.setLayout(ImageItem.LAYOUT_DEFAULT | Item.LAYOUT_2);//GEN-END:|24-getter|1|24-postInit
        // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return login;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: password ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of password component.
     * @return the initialized component instance
     */
    public TextField getPassword() {
        if (password == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            password = new TextField("password", "hugo", 32, TextField.ANY | TextField.PASSWORD);//GEN-BEGIN:|25-getter|1|25-postInit
            password.setLayout(ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER);//GEN-END:|25-getter|1|25-postInit
        // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return password;
    }
    //</editor-fold>//GEN-END:|25-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|26-getter|0|26-preInit
    /**
     * Returns an initiliazed instance of alert component.
     * @return the initialized component instance
     */
    public Alert getAlert() {
        if (alert == null) {//GEN-END:|26-getter|0|26-preInit
            // write pre-init user code here
            alert = new Alert("alert", "Invalid Login!!", null, null);//GEN-BEGIN:|26-getter|1|26-postInit
            alert.setTimeout(Alert.FOREVER);//GEN-END:|26-getter|1|26-postInit
        // write post-init user code here
        }//GEN-BEGIN:|26-getter|2|
        return alert;
    }
    //</editor-fold>//GEN-END:|26-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: loginValidation ">//GEN-BEGIN:|28-if|0|28-preIf
    /**
     * Performs an action assigned to the loginValidation if-point.
     */
    public void loginValidation() {//GEN-END:|28-if|0|28-preIf
        String login = getLogin().getString();
        String password = getPassword().getString();
    if (login != null//GEN-BEGIN:|28-if|1|29-preAction
            && password != null
            && login.equals("Hugo")
            && password.equals("hugo") ) {//GEN-END:|28-if|1|29-preAction
        // pre-action user code here
        switchDisplayable(null, getWelcomeForm());//GEN-LINE:|28-if|2|29-postAction
// write post-action user code here
    } else {//GEN-LINE:|28-if|3|30-preAction
        // write pre-action user code here
        switchDisplayable(null, getAlert());//GEN-LINE:|28-if|4|30-postAction
// write post-action user code here
    }//GEN-LINE:|28-if|5|28-postIf
// enter post-if user code here
    }//GEN-BEGIN:|28-if|6|
    //</editor-fold>//GEN-END:|28-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|36-getter|1|36-postInit
        // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|36-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: welcomeForm ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of welcomeForm component.
     * @return the initialized component instance
     */
    public Form getWelcomeForm() {
        if (welcomeForm == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            welcomeForm = new Form("form1", new Item[] { getImageItem() });//GEN-BEGIN:|33-getter|1|33-postInit
            welcomeForm.addCommand(getExitCommand1());
            welcomeForm.addCommand(getMapScreenCommand());
            welcomeForm.setCommandListener(this);//GEN-END:|33-getter|1|33-postInit
        // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return welcomeForm;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageItem ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of imageItem component.
     * @return the initialized component instance
     */
    public ImageItem getImageItem() {
        if (imageItem == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            imageItem = new ImageItem("VadeMecum Client", null, ImageItem.LAYOUT_DEFAULT, "Welcome!!");//GEN-LINE:|34-getter|1|34-postInit
        // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return imageItem;
    }
    //</editor-fold>//GEN-END:|34-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: pimBrowser ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of pimBrowser component.
     * @return the initialized component instance
     */
    public PIMBrowser getPimBrowser() {
        if (pimBrowser == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            pimBrowser = new PIMBrowser(getDisplay(), PIM.CONTACT_LIST);//GEN-BEGIN:|39-getter|1|39-postInit
            pimBrowser.setTitle("pimBrowser");
            pimBrowser.addCommand(PIMBrowser.SELECT_PIM_ITEM);
            pimBrowser.setCommandListener(this);//GEN-END:|39-getter|1|39-postInit
        // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return pimBrowser;
    }
    //</editor-fold>//GEN-END:|39-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: mapScreenCommand ">//GEN-BEGIN:|49-getter|0|49-preInit
/**
 * Returns an initiliazed instance of mapScreenCommand component.
 * @return the initialized component instance
 */
public Command getMapScreenCommand() {
    if (mapScreenCommand == null) {//GEN-END:|49-getter|0|49-preInit
        // write pre-init user code here
        mapScreenCommand = new Command("Mapa", Command.SCREEN, 0);//GEN-LINE:|49-getter|1|49-postInit
    // write post-init user code here
    }//GEN-BEGIN:|49-getter|2|
    return mapScreenCommand;
}
//</editor-fold>//GEN-END:|49-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
